/**
 * 建议尽量全字段或者区域多字段进行匹配, 避免误触
 */
// Import("ocr.js")
// Import("utils.js")
// Import("ggUtils.js")


// SystemPrint.log(app.getTopPackName())

var APP_NAME_XFDJ = 'XFDJ'
var PACK_NAME_XFDJ = 'com.kwai.theater'
var MAIN_PAGE_FLAG_XFDJ = '首页'
var ENTER_CENTER_PAGE_FLAG_XFDJ = '进入任务中心的标志位'
var CENTER_PAGE_FLAG_XFDJ = '现金'

var xf_zsjb = parseInt(uiText.findByID(控件ID = 'xf_zs_jb').getText())


function close弹窗() {
    //找图返回目标数组
    var detectsclose = opencv.findImagesEx('图色538588.cv');
    if (detectsclose != null) {
        agentOrHidClick(detectsclose[0])
    }
}

function skip_主界面的弹窗_XFDJ() {
    if (!hasExcuteFunc('newp_XFDJ') && (getExcuteFuncTimes('start_收集金币_XFDJ') <= 3 || getExcuteFuncTimes('start_浏览任务_XFDJ') <= 3)) {
        if (ocrText('新人7天', '见面礼')) {
            agentOrHidClick(ocrText('领取', 1, () => {
                setExcuteFunc('newp_XFDJ')
            }))

            close弹窗()
        }
    }
}


function skip_任务中心的弹窗_XFDJ() {
    close弹窗()
}

// sample看XF广告(true)
// whilewatch_看广告_XFDJ()
function sample看XF广告(isAutoDown) {
    if (isAutoDown == undefined) isAutoDown = false
    log('去获取广告时长')
    ocrWaitForText('s后', '秒后', 's', '领取奖励', 5, 2)

    addExcuteFuncTimes('xf_ksp_times')
    // if (hasExcuteFunc('xf_low_jb')) {
    //     log('包不满跳应用')
    //     var falgdk = ocrWaitForMoreTextMillisecondInRegion(0, 0.75, 1, 1, 4, '立即打开', '打开应用')
    //     if (falgdk == null) {
    //         falgdk = ocrTextMoreComplete('打开')
    //     }
    //     agentOrHidClick(falgdk, 5, () => {
    //         log('点击同意')
    //         var falg = ocrTextMoreComplete('同意', '同意并继续', '|同意井继续', '打开')
    //         if (falg == null) {
    //             falg = ocrText('同意并', '同意隐', '意隐私')
    //         }
    //         if (falg == null) {
    //             sleep.second(20)
    //         }
    //         agentOrHidClick(falg, 23)
    //         app.openAppv2(PACK_NAME_XFDJ)
    //         sleep.second(1)
    //     })
    // }


    // ocrTextTrue('立即下载')xf_jg_times
    var is内转 = false
    var xf_间隔次数 = parseInt(uiText.findByID(控件ID = 'xf_jg_times').getText())
    log('下载间隔次数' + getExcuteFuncTimes('xf_total_times'))
    if (isAutoDown && (hasExcuteFunc('xf_low_jb') || (getExcuteFuncTimes('xf_total_times') >= xf_间隔次数 || getExcuteFuncTimes('xf_total_times') == 0) || getExcuteFuncTimes('xf_ksp_times') < 3)) {
        log('去判断是否为下载类广告' + getExcuteFuncTimes('xf_total_times'))

        var allres = ocrAllRes()
        if (!isBetweenTime(22, 24)) {
            if (
                ocrWaitForMoreTextMillisecond('西瓜视频', '抖音', '喜番', '头条极速', '悟空', 4, 500)
                || getExcuteFuncTimes('xf_no_down') > 6
                || ocrTextMoreByRes(allres, '火山', '汽水', '红果', '今日头条', '壁纸', '纸大全', '萌象', '悟空', '空浏览')
                || ocrTextMoreByRes(allres, '抖', '西瓜视频', '抖音', '喜番', '头条极速', '今日头条', '广电')
                || ocrTextMoreByRes(allres, '联通', '电信', '移动', '瓜视频', '条极速', '果短', '百度', '会员任选')) {
                agentOrHidback()
                sleep.second(1)
                addExcuteFuncTimes('xf_reload_times')
                if (getExcuteFuncTimes('xf_reload_times') > 3) {
                    addExcuteFuncTimes('xf_no_down', 0)
                }
                agentOrHidClick(ocrWaitForMoreTextMillisecond('换个视', '个视频'))
                sample看XF广告(isAutoDown)
                return
            }
        }


        var downFlag = ocrWaitForText('立即下载', '点击下载', '免费安装', '立即安装', '下载游戏', '免费下', '安装并打', 3, 2)
        if ((downFlag || ocrTextMoreByRes(allres, '下载应用', '一键下载'))
            && !ocrTextMoreByRes(allres, '永劫', '无间', '网易', '梦幻', '西游', '王者', '大咖', '原神', '鸣潮')
            && !ocrTextMoreByRes(allres, '问界', '问剑', '快手', '快手极速版', '逆水', '水寒', '英语天', '天练', '喜番')
            && !ocrTextMoreByRes(allres, '抖', '问剑', '地下城', '三角', '角洲', '西瓜视频', '抖音', '万国', '觉醒')
            && !ocrTextMoreByRes(allres, '火山', '汽水', '红果', '今日头条', '壁纸', '纸大全', '萌象', '悟空', '空浏览')
            && !ocrTextMoreByRes(allres, '讯地图', '腾讯地')) {
            log('点击下载')
            addExcuteFuncTimes('xf_no_down', 0)
            agentOrHidClick(downFlag, 1, () => {
                agentOrHidClick(ocrWaitForMoreTextMillisecond('立即下载', '点击下载', '免费安装', '立即安装', '下载游戏', '免费下', '安装并打'))
                sleep.second(1)
                addExcuteFuncTimes('xf_total_times', 0)
                Clipboard.copy('123456mm')

                log('点击安装')
                // ocrTextTrue('安装(')
                if (ocrWaitForMoreTextMillisecond('安装(', '报告', '次安装', 4, 500)) {
                    log('是外部连接下载')
                    var allres = ocrAllRes()
                    if (ocrTextMoreByRes(allres, '报告', '次安装')) {
                        if (ocrTextMoreByRes(allres, '应用详情')) {
                            agentOrHidClick(ocrTextInRegion(0, 0.8, 0, 0.25, '安装'))
                        }
                        agentOrHidClick(ocrTextInRegion(0, 0.8, 0.7, 1, '安装'))
                        log('等待完成打开')
                        ocrWaitForText('完成', '打开', 60, 2)
                    }
                    agentOrHidClick(ocrWaitForText('安装(', 2, 1), 2, () => {
                        log('移动顶部')
                        move2TopSmall(1)

                        log('等待完成打开')
                        ocrWaitForText('完成', '打开', 60, 2)
                    })
                } else {
                    log('判断是否是喜番内的下载')
                    var isxfgg = false

                    var detects广告标识 = opencv.findImagesEx('图色205081.cv');
                    if (detects广告标识 != null) {
                        isxfgg = true
                        is内转 = true
                    } else {
                        if (ocrWaitForText('查找类', '已成功', '成功领取', 's后', '秒后', '领取奖励', '继续安装')) {
                            isxfgg = true
                            is内转 = true
                        }
                    }
                    if (isxfgg) {
                        log('喜番内的下载等待继续安装')
                        agentOrHidClick(ocrWaitForText('继续安装', '任然安装', 40, 3), 1, () => {
                            if (ocrWaitForMoreTextMillisecond('应用权', '用权限', '扫描', '正在', '解析', 4)) {
                                ocrWaitForText('未发现', '发现风险', '打开应用', '安装完成', 30, 2)
                                sleep.second(1)
                                agentOrHidClick(ocrText('继续安装', '立即安装', '任然安装'))
                            } else {
                                //找图返回目标数组
                                var detectscheck = opencv.findImagesEx('图色341992.cv');
                                if (detectscheck != null) {
                                    agentOrHidClick(detectscheck[0])
                                }
                                agentOrHidClick(ocrText('继续安装', '立即安装', '任然安装'))
                                if (!ocrText('介绍', '评论', '次安装', '人工', '搜索')) {
                                    log('点击输入密码')
                                    click(0.17, 0.68)

                                    log('点击安装')
                                    click(0.73, 0.57)

                                }
                            }



                            log('等待完成打开')
                            ocrWaitForText('完成', '打开', '发现恶意', '恶意应用', 60, 2)
                            agentOrHidClick(ocrText('完成'))
                        })
                    }

                }



                // log('返回喜番')
                // agentOrHidback()
                // sleep.second(2)
                log('准备回到喜番打开应用')
                if ((getExcuteFuncTimes('xf_open_app2_error') <= 2 && hasExcuteFunc('xf_open_app2')) || getExcuteFuncTimes('open_xf_way') == 1) {
                    app.openAppv2(PACK_NAME_XFDJ)
                    sleep.second(1)
                    addExcuteFuncTimes('open_xf_way')
                    if (!ocrText('已成功领取', '已成功', '成功领取', 's后', '秒后', '领取奖励')) {
                        var detects广告标识 = opencv.findImagesEx('图色205081.cv');
                        if (detects广告标识 == null) {
                            addExcuteFuncTimes('xf_open_app2_error')
                            if (getExcuteFuncTimes('xf_open_app2_error') >= 2) {
                                setExcuteFunc('xf_open_app2', false)
                            }
                            enter_任务中心_XFDJ()
                            return
                        }
                    } else {
                        setExcuteFunc('xf_open_app2')
                        addExcuteFuncTimes('open_xf_way', 1)
                    }
                }
                if ((getExcuteFuncTimes('xf_open_app1_error') <= 2 && hasExcuteFunc('xf_open_app1')) || getExcuteFuncTimes('open_xf_way') == 0) {
                    app.openApp(PACK_NAME_XFDJ)
                    sleep.second(1)
                    addExcuteFuncTimes('open_xf_way')
                    if (!ocrText('已成功领取', '已成功', '成功领取', 's后', '秒后', '领取奖励')) {
                        var detects广告标识 = opencv.findImagesEx('图色205081.cv');
                        if (detects广告标识 == null) {
                            addExcuteFuncTimes('xf_open_app1_error')
                            if (getExcuteFuncTimes('xf_open_app1_error') >= 2) {
                                setExcuteFunc('xf_open_app1', false)
                            }
                            enter_任务中心_XFDJ()
                            return
                        }
                    } else {
                        setExcuteFunc('xf_open_app1')
                        addExcuteFuncTimes('open_xf_way', 0)
                    }
                }

                if ((!hasExcuteFunc('xf_open_app2') && !hasExcuteFunc('xf_open_app1') && getExcuteFuncTimes('open_xf_way') >= 2)
                    || (getExcuteFuncTimes('xf_open_app2_error') > 2 && getExcuteFuncTimes('xf_open_app1_error') > 2)) {
                    agentOrHidRecents()
                    sleep.second(2)
                    var backFlag = ocrWaitForText('喜番', '番免')
                    if (backFlag == null) {
                        agentSwipRight()
                        sleep.second(2)
                        backFlag = ocrWaitForText('喜番', '番免')
                    }
                    agentOrHidClick(backFlag)

                    if (!ocrText('已成功领取', '已成功', '成功领取', 's后', '秒后', '领取奖励')) {
                        var detects广告标识 = opencv.findImagesEx('图色205081.cv');
                        if (detects广告标识 == null) {
                            if (!hasExcuteFunc('xf_open_app1_error')) {
                                app.openApp(PACK_NAME_XFDJ)
                                sleep.second(2)
                                detects广告标识 = opencv.findImagesEx('图色205081.cv');
                                if (detects广告标识 == null) {
                                    setExcuteFunc('xf_open_app1_error')
                                    enter_任务中心_XFDJ()
                                    return
                                } else {
                                    setExcuteFunc('xf_open_app1')
                                }
                            } else {
                                app.openAppv2(PACK_NAME_XFDJ)
                                sleep.second(2)
                                detects广告标识 = opencv.findImagesEx('图色205081.cv');
                                if (detects广告标识 == null) {
                                    enter_任务中心_XFDJ()
                                    return
                                } else {
                                    setExcuteFunc('xf_open_app2')
                                }
                            }

                        }
                    }
                }

                log('从喜番点击打开应用')
                var falgdk = ocrTextMoreComplete('打开')
                if (falgdk == null) {
                    falgdk = ocrTextMoreInRegion(0, 0.75, 1, 1, '立即打开', '立即下载', '点击下载', '打开应用')
                }
                agentOrHidClick(falgdk, 5, () => {
                    log('点击同意')

                    var falg = ocrTextMoreComplete('同意', '同意并继续', '|同意井继续', '打开')
                    if (falg == null) {
                        falg = ocrText('同意并', '同意隐', '意隐私', '同意')
                    }
                    if (falg == null) {
                        sleep.second(20)
                    }
                    agentOrHidClick(falg, 60)
                    agentOrHidClick(ocrTextMoreComplete('允许', '已满14岁', '已满18岁'), 20)
                })
                // agentOrHidback()
                // sleep.second(2)

                // backUntilFindView(() => {
                //     return ocrText('已成功领取', '已成功', '成功领取', 's后', '秒后', '领取奖励')
                // }, 3)
                log('准备回到喜番继续观看视频')
                if (getExcuteFuncTimes('xf_open_app2_error') <= 2 && hasExcuteFunc('xf_open_app2')) {
                    app.openAppv2(PACK_NAME_XFDJ)
                    sleep.second(1)
                }
                if (getExcuteFuncTimes('xf_open_app1_error') <= 2 && hasExcuteFunc('xf_open_app1')) {
                    app.openApp(PACK_NAME_XFDJ)
                    sleep.second(1)
                }
                if ((!hasExcuteFunc('xf_open_app2') && !hasExcuteFunc('xf_open_app1'))
                    || (getExcuteFuncTimes('xf_open_app2_error') > 2 && getExcuteFuncTimes('xf_open_app1_error') > 2)) {
                    agentOrHidRecents()
                    sleep.second(2)
                    var backFlag = ocrWaitForText('喜番', '番免')
                    if (backFlag == null) {
                        agentSwipRight()
                        sleep.second(2)
                        backFlag = ocrWaitForText('喜番', '番免')
                    }
                    agentOrHidClick(backFlag)

                    if (!ocrText('已成功领取', '已成功', '成功领取', 's后', '秒后', '领取奖励')) {
                        var detects广告标识 = opencv.findImagesEx('图色205081.cv');
                        if (detects广告标识 == null) {
                            if (!hasExcuteFunc('xf_open_app1_error')) {
                                app.openApp(PACK_NAME_XFDJ)
                                sleep.second(2)
                                detects广告标识 = opencv.findImagesEx('图色205081.cv');
                                if (detects广告标识 == null) {
                                    setExcuteFunc('xf_open_app1_error')
                                    enter_任务中心_XFDJ()
                                    return
                                } else {
                                    setExcuteFunc('xf_open_app1')
                                }
                            } else {
                                app.openAppv2(PACK_NAME_XFDJ)
                                sleep.second(2)
                                detects广告标识 = opencv.findImagesEx('图色205081.cv');
                                if (detects广告标识 == null) {
                                    enter_任务中心_XFDJ()
                                    return
                                } else {
                                    setExcuteFunc('xf_open_app2')
                                }
                            }

                        }
                    }
                }
                // app.openAppv2(PACK_NAME_XFDJ)


                agentOrHidClick(ocrWaitForText('继续观看', '继续', '取消', 2, 1))
            })
        } else {
            addExcuteFuncTimes('xf_no_down')
        }

        addExcuteFuncTimes('xf_auto_down')
    } else {
        addExcuteFuncTimes('xf_no_down')
    }

    addExcuteFuncTimes('xf_reload_times', 0)
    addExcuteFuncTimes('xf_total_times')

    var detectshomePage = opencv.findImagesEx('图色388013.cv');
    if (detectshomePage != null) {
        start_app_XFDJ()
        sleep.second(1)
    }

    var sleeptime = getDectNumber(ocrWaitForText('s后', '秒后', 's', '领取奖励', 5, 2))
    if (sleeptime < 5 || sleeptime > 40) {
        sleeptime = 3
    }
    logSleepTime('广告时长' + sleeptime, sleeptime)

    log('等待界面不再变化')
    var rgbs
    whilestep((stepTimes, alltimes, canBreak) => {
        rgbs = get界面采样点()
        var allres = ocrAllRes()

        agentOrHidClick(ocrTextMoreByRes(allres, '放弃福利'))
        if (ocrTextMoreByRes(allres, '点击直接', '更快')) {
            agentClickRightOffectRight(ocrTextMoreByRes(allres, '继续看视频', '秒拿奖励'), 10)
        }
        if (ocrTextMoreByRes(allres, '恭喜获得奖励', '已成功', '成功领取', '功领取')) {
            canBreak()
        }
    }, 30, 2, (canBreak) => {
        if (check界面是否不再变化(rgbs)) {
            if (!ocrText('s后', '秒后', 's', '秒', '恭喜获得奖励')) {
                log('界面不再变化!!!')
                canBreak()
            }
        }
    })


    log('广告应该结束')
    if (ocrText('我的金币', '日常任务', '领金币', '看视频领', '逛街', '签到', '金币:')) {
        logSleep('已返回主页面')
        return
    }
    var closeflag = ocrWaitForText('已成功领取', '已成功', '成功领取', '已成', '功领取', is内转 ? 3 : 15, 2)
    if (closeflag != null) {
        //找图返回目标数组
        var detectsclose = opencv.findImagesEx('图色131243.cv');
        if (detectsclose != null) {
            agentOrHidClick(detectsclose[0])
        } else {
            agentOrHidback()
            sleep.second(2)
        }
    } else {
        log('广告未检测结束弹窗')
        agentOrHidClick(ocrText('放弃福利'))
        sleeptime = getDectNumber(ocrWaitForText('s后', '秒后', 's', '领取奖励', 3, 2))
        if (sleeptime < 5 || sleeptime > 30) {
            sleeptime = 5
        }
        logSleepTime('广告时长' + sleeptime, sleeptime)
        // agentOrHidback()
        // sleep.second(2)
        closeflag = ocrWaitForText('已成功领取', '已成功', '成功领取', '已成', '功领取', 3, 2)
        if (closeflag != null) {
            //找图返回目标数组
            var detectsclose = opencv.findImagesEx('图色131243.cv');
            if (detectsclose != null) {
                agentOrHidClick(detectsclose[0])
            } else {
                agentOrHidback()
                sleep.second(2)
            }
        } else {
            log('强制返回')
            agentOrHidback()
            sleep.second(2)

            if (!ocrText('已成功领取', '已成功', '成功领取', 's后', '秒后', '领取奖励')) {
                var detects广告标识 = opencv.findImagesEx('图色205081.cv');
                if (detects广告标识 == null) {
                    enter_任务中心_XFDJ()
                    return
                }
            }
        }
    }


    if (ocrWaitForText('再看', '看一个', '再领')) {
        var flagmore = ocrText('+')
        var getjb = getDectNumber(flagmore)
        if (getjb < 3000) {
            setExcuteFunc('xf_low_jb')
        } else {
            setExcuteFunc('xf_low_jb', false)
        }
        log('获取到金币' + getjb)
        if (getjb >= xf_zsjb) {
            // ocrTextTrue('立即领取')
            agentOrHidClick(ocrTextInRegion(0, 0.4, 1, 0.78, '立即领取'), 2, () => {
                sample看XF广告(isAutoDown)
            })
        } else {
            setExcuteFuncInTime一段时间内不再执行('start_收集金币_XFDJ', 60 * 60)
            agentOrHidClick(ocrText('放弃'))
        }

    } else {
        log('判断是否在任务页')
        if (!ocrText('日常任务', '金币:', '现金:', '到饭点', '多看多得')) {
            var detectsclose = opencv.findImagesEx('图色131243.cv');
            if (detectsclose != null) {
                agentOrHidClick(detectsclose[0])
            }

            agentOrHidClick(ocrText('放弃奖', '弃奖励'))
        }
    }


}



function back2center_XFDJ() {
    // if (!backUntilFindView(() => ocrText('日常任务', '金币:', '抵用金:', '点就领'))) {
    //     enter_任务中心_XFDJ(true)
    // }
    array.forEach(element => {
        // Ddddd
    });
}


function move2顶部() {
    log('移动到顶部')
    move2Top(2, 300, '我的现金', '自动兑换')
}

// 启动任务
function start_app_XFDJ() {
    var isuccess = startApp(PACK_NAME_XFDJ)
    if (!isuccess) {
        log('喜番未登陆')
        stopAllApp()
        return false
    }

    // 跳过首页广告
    expectView(() => {
        return ocrTextMoreInRegion(0, 0.88, 1, 1, MAIN_PAGE_FLAG_XFDJ, '热榜', '推荐')
    }, (times) => {
        skip_主界面的弹窗_XFDJ()

        if (times > 6) {
            agentOrHidback()
            sleep.millisecond(500)
            // todo
        }
        sleep.millisecond(500)
    }, (findView) => {
        // agentOrHidClick(findView)
    }, null, 6)

    return true
}

// 进入任务中心
function enter_任务中心_XFDJ(iskip) {
    log('进入任务中心')

    if (iskip == undefined) iskip = false

    // 进入任务中心
    expectView(() => {
        var ocr1福 = opencv.OCREx('图色574277.cv');
        if (ocr1福 != null) {
            return ocr1福.getAllDetect()[0]
        }
        return null
    }, (times) => {
        skip_主界面的弹窗_XFDJ()

        sleep.millisecond(500)
        SystemPrint.warn('未找到主界面期望标志位')
    }, (findView, stepTimes) => {
        if (stepTimes == 1) {
            skip_主界面的弹窗_XFDJ()
        }
        agentOrHidClick(findView)
    }, () => {
        // 失败后的方案 或者其它方案进入
        clickOffectY(5 / 8, 1, 60)
        agentOrHidClick(ocrTextInRegion(0.3, 0.85, 1, 1, '福利'))
        //找色返回目标数组
        // var detects福袋 = opencv.findMultiColorEx('图色657823.cv');
        // if (detects福袋 != null) {
        //     agentOrHidClick(detects福袋[0])
        // }
    }, 6)


    // if (!iskip) {
    //     // 期望标志位
    //     expectView(() => {
    //         return ocrText(CENTER_PAGE_FLAG_XFDJ, '金币', '签到领', '福利中心', '每日看剧', '日常任务')
    //     }, (times) => {
    //         skip_任务中心的弹窗_XFDJ()

    //         if (times > 3) {
    //             agentOrHidback()
    //             sleep.millisecond(500)
    //             // todo 未找到后 返回怎么做
    //         }
    //         sleep.millisecond(500)
    //         SystemPrint.warn('未找到任务界面期望标志位')
    //     }, (findView) => {
    //         // agentOrHidClick(findView)
    //     }, null, 3)
    // }
}


function open_签到_XFDJ() {
    printinfoOld('open_签到_XFDJ-------------------------------')


    if (hasExcuteFunc('open_签到_XFDJ') || getExcuteFuncTimes('open_签到_XFDJ') > 3) {
        return
    }

    if (getExcuteFuncTimes('open_签到_XFDJ') <= 1) {
        agentOrHidClick(ocrWaitForText('立即签到', '立即领取+', '立即领取'), 2, () => {
            var spFlag = ocrWaitForText('看视频最高', '视频最高领')
            if (getDectNumber(spFlag) > xf_zsjb) {
                agentOrHidClick(spFlag)
                sample看XF广告()
            } else {
                close弹窗()
            }
        })
    } else {
        skip_任务中心的弹窗_XFDJ()

        // moveUp2FindWidTxtMore((findView) => {
        //     agentOrHidClick(findView)
        //     var spFlag = ocrWaitForText('看视频最高', '视频最高领')
        //     if (getDectNumber(spFlag) > 1500) {
        //         agentOrHidClick(spFlag)
        //         sample看XF广告()
        //     } else {
        //         close弹窗()
        //     }
        //     setExcuteFunc('open_签到_XFDJ')
        // }, '签到领金币', '明日签到')
    }

    addExcuteFuncTimes('open_签到_XFDJ')
}

function open_开宝箱_XFDJ() {
    printinfoOld('open_开宝箱_XFDJ-------------------------------')
    if (hasExcuteFuncInTime是否在一段时间内执行过('open_开宝箱_XFDJ') || getExcuteFuncTimes('open_开宝箱_XFDJ') > 35) {
        return
    }
    skip_任务中心的弹窗_XFDJ()

    //找色返回目标数组
    var detects宝箱 = opencv.findMultiColorEx('图色609228.cv');
    if (detects宝箱 != null) {
        agentOrHidClick(detects宝箱[0], 1, () => {
            var spFlag = ocrWaitForText('看视频最高', '视频最高领')
            if (spFlag) {
                addExcuteFuncTimes('open_开宝箱_XFDJ')
            }
            if (getDectNumber(spFlag) >= 1500) {
                // watchGG_多次看广告(spFlag)
                agentOrHidClick(spFlag)
                sample看XF广告()


            } else {
                close弹窗()
            }
        })
    }

    sleep.second(1)
    // 获取等待时长
    var dexts = ocrTextInRegion(0.5, 0.5, 1, 1, '领金币')
    if (dexts) {
        var sleepMin = getDectNumber(dexts)
        if (sleepMin >= 30) {
            sleepMin = 30
        }
        setExcuteFuncInTime一段时间内不再执行('open_开宝箱_XFDJ', sleepMin * 60 + 59)
        // if (hasExcuteFunc('watch_看广告_XFDJ')) {
        //     setExcuteFuncInTime一段时间内不再执行('start_收集金币_XFDJ', sleepMin * 60 + 59)
        // }
    }

}

function watch_看广告_XFDJ() {
    printinfoOld('watch_看广告_XFDJ-------------------------------')


    if (hasExcuteFunc('watch_看广告_XFDJ')) {
        return
    }
    skip_任务中心的弹窗_XFDJ()

    move2顶部(2)
    // 只执行一次
    moveUp2FindWidTxtMore((findView) => {
        if (ocrText('今日已获得', '所有奖励')) {
            setExcuteFunc('watch_看广告_XFDJ')
            return
        }
        agentOrHidClick(findView)
        sample看XF广告()
        addExcuteFuncTimes('watch_看广告_XFDJ')
    }, '看视频领', '看广告得', '单日最高赚', '看广告领海量')
}

function whilewatch_看广告_XFDJ() {
    log('循环看视频')

    if (hasExcuteFunc('whilewatch_看广告_XFDJ')) {
        return
    }
    skip_任务中心的弹窗_XFDJ()

    move2顶部(2)
    // 只执行一次
    moveUp2FindWidTxtMore((findView) => {
        if (ocrText('今日已获得', '所有奖励')) {
            log('看视频任务已完成?')
            setExcuteFunc('whilewatch_看广告_XFDJ')
            return
        }

        log('准备开始看视频')
        var flagjb = getDectNumber(findView)
        if (flagjb >= xf_zsjb || flagjb == 0) {
            var flag = ocrText('看视频领', '看广告得', '单日最高赚', '看广告领海量')
            while (flag) {
                agentOrHidClick(flag)
                if (isEnableSwitch('xf_auto_down')) {
                    sample看XF广告(true)
                } else {
                    sample看XF广告()
                }

                log('结束广告前判断是否正常结束')
                var detects广告标识 = opencv.findImagesEx('图色205081.cv');
                if (detects广告标识 != null) {
                    agentOrHidback()
                    sleep.second(2)
                    agentOrHidClick(ocrText('放弃奖', '弃奖励'))
                }

                if (!ocrText('我的金币', '日常任务', '领金币', '看视频领', '逛街', '签到')) {
                    try {
                        app.openApp(PACK_NAME_XFDJ)
                    } catch (err) {
                        app.openAppv2(PACK_NAME_XFDJ)
                    }
                    sleep.second(1)
                    enter_任务中心_XFDJ()
                }

                log('开宝箱')
                var detectsbx = opencv.findImagesEx('图色156227.cv');
                if (detectsbx != null) {
                    agentOrHidClick(detectsbx[0])

                    if (!ocrText('后开启')) {
                        flag = ocrTextInRegion(0, 0.5, 1, 0.75, '看视频领', '看广告得', '单日最高赚', '看广告领海量')
                        agentOrHidClick(flag)
                        if (isEnableSwitch('xf_auto_down')) {
                            sample看XF广告(true)
                        } else {
                            sample看XF广告()
                        }

                        log('结束广告前判断是否正常结束')
                        var detects广告标识 = opencv.findImagesEx('图色205081.cv');
                        if (detects广告标识 != null) {
                            agentOrHidback()
                            sleep.second(2)
                            agentOrHidClick(ocrText('放弃奖', '弃奖励'))
                        }

                        if (!ocrText('我的金币', '日常任务', '领金币', '看视频领', '逛街', '签到')) {
                            try {
                                app.openApp(PACK_NAME_XFDJ)
                            } catch (err) {
                                app.openAppv2(PACK_NAME_XFDJ)
                            }
                            sleep.second(1)
                            enter_任务中心_XFDJ()
                        }
                    } else {
                        skip_任务中心的弹窗_XFDJ()
                    }

                }

                log('新的开始看视频')
                flag = ocrText('看视频领', '看广告得', '单日最高赚', '看广告领海量')
                if (!flag) {
                    move2顶部(2)
                    moveUp2FindWidTxtMore((findView) => {
                        flag = findView
                        addExcuteFuncTimes('xf_error_times', 0)
                    }, '看视频领', '看广告得', '单日最高赚', '看广告领海量', () => {
                        return ocrText('工作时间', '客服联系方式')
                    }, () => {
                        addExcuteFuncTimes('xf_error_times')
                    })
                } else {
                    addExcuteFuncTimes('xf_error_times', 0)
                }

                if (getExcuteFuncTimes('xf_error_times') >= 2) {
                    break
                }

                flagjb = getDectNumber(flag)
                if (flagjb < xf_zsjb && flagjb != 0) {
                    log('金币不达标去挂剧')
                    // agentOrHidClick(ocrText())
                    clickOffectY(3 / 8, 1, 60)
                    ocrWaitForText('新剧')
                    click(0.5, 0.5)
                    log('挂剧10分钟')
                    sleep.second(10 * 60)

                    break
                }
            }
        } else {
            log('金币不达标去挂剧')
            // agentOrHidClick(ocrText())
            clickOffectY(3 / 8, 1, 60)
            ocrWaitForText('新剧')
            click(0.5, 0.5)
            log('挂剧10分钟')
            sleep.second(10 * 60)
        }

        addExcuteFuncTimes('whilewatch_看广告_XFDJ')
    }, '看视频领', '看广告得', '单日最高赚', '看广告领海量')
}

function watch_看短剧_XFDJ() {
    printinfoOld('watch_看短剧_XFDJ-------------------------------')


    if (hasExcuteFunc('watch_看短剧_XFDJ') || getExcuteFuncTimes('watch_看短剧_XFDJ') > 8) {
        return
    }
    skip_任务中心的弹窗_XFDJ()

    move2顶部(2)
    // 只执行一次
    moveUpHigher2FindWidTxtMore((findView) => {
        if (isInNearBy(findView, '已完成')) {
            setExcuteFunc('watch_看短剧_XFDJ')
            return
        }

        var flag = ocrText('可领取')
        if (flag) {
            addExcuteFuncTimes('watch_看短剧_XFDJ')
            agentOrHidClick(flag)
            var spFlag = ocrWaitForText('看视频最高', '视频最高领')
            if (getDectNumber(spFlag) >= 1500) {
                // watchGG_多次看广告(spFlag)
                agentOrHidClick(spFlag)
                sample看XF广告()


            } else {
                close弹窗()
            }
        }

    }, '看短剧领金币', '短剧领金币', '看短剧领')
}

// 进行浏览任务
function start_浏览任务_XFDJ() {
    if (isEnableSwitch('xf_bll')) {
        logSleep('喜番不浏览')
        return
    }
    if (hasExcuteFunc('watch_看短剧_XFDJ') || getExcuteFuncTimes('start_浏览任务_XFDJ') >= 5) {
        return
    }
    if (!start_app_XFDJ()) {
        return
    }

    skip_主界面的弹窗_XFDJ()

    //找色返回目标数组
    var detects播放界面 = opencv.findMultiColorEx('图色345651.cv');
    if (detects播放界面 == null) {
        agentOrHidClick(ocrText('继续观', '古风·', '武侠·', '复仇·', '穿越·', '家庭·', '都市·'))
    }


    if (detects播放界面 || ocrText('选集', '倍速', '免费看全集', '来一发')) {
        whilestep((stepTimes) => {
            if (stepTimes % 2 == 0 || stepTimes % 3 == 0) {
                if (ocrText('上滑继续', '可上滑')) {
                    agentSwipBig()
                }
            }
            if (stepTimes % 5 == 0) {
                agentOrHidClick(ocrText('点击重试'))
            }
            if (stepTimes % 30 == 0) {
                agentSwipBig()
            }
        }, 20 * 60, 10)

        addExcuteFuncTimes('start_浏览任务_XFDJ')
    }
}

function start_刷贷广告_XFDJ() {
    log('喜番刷贷广告')

    currentAppPackName = PACK_NAME_XFDJ
    // closeApp(PACK_NAME_XFDJ)

    if (!start_app_XFDJ()) {
        return
    }

    enter_任务中心_XFDJ()


    function find刷贷GG() {
        ocrWaitForText('s后', '秒后', 's', '领取奖励', 3, 2)
        if (ocrText('贷款', '贷', '保险', '疾', '投保')) {
            log('找到目标广告')
        } else {
            agentOrHidback()

            agentOrHidClick(ocrWaitForText('个视频', '换个视'))
            find刷贷GG()
        }
    }

    log('宝箱刷')
    var detectsbx = opencv.findImagesEx('图色156227.cv');
    if (detectsbx != null) {
        agentOrHidClick(detectsbx[0])

        var flag = ocrWaitForMoreTextMillisecondInRegion(0, 0.5, 1, 0.75, 6, '看视频领', '看广告得', '单日最高赚', '看广告领海量')
        agentOrHidClick(flag)

        find刷贷GG()
    }
}

function start_只看视频广告_XFDJ() {
    log('喜番只看视频广告')

    currentAppPackName = PACK_NAME_XFDJ
    // closeApp(PACK_NAME_XFDJ)

    if (!start_app_XFDJ()) {
        return
    }

    enter_任务中心_XFDJ()

    whilewatch_看广告_XFDJ()

    stopAllApp()
}

function start_收集金币_XFDJ() {
    if (isEnableSwitch('xf_bsj')) {
        logSleep('喜番不刷金')
        return
    }
    printinfoOld('start_收集金币_XFDJ')
    if (hasExcuteFuncInTime是否在一段时间内执行过('start_收集金币_XFDJ')) {
        return
    }

    if (!isAllow允许登录(PACK_NAME_XFDJ)) {
        if (getExcuteFuncTimes('start_收集金币_XFDJ') == 0) {

            printwarn('xxx启动失败或被屏蔽')
            sleep.second(10)

            addExcuteFuncTimes('start_收集金币_XFDJ')
        }
        return
    }
    currentAppPackName = PACK_NAME_XFDJ
    // closeApp(PACK_NAME_XFDJ)

    if (!start_app_XFDJ()) {
        return
    }

    addExcuteFuncTimes('start_收集金币_XFDJ')

    enter_任务中心_XFDJ()

    open_签到_XFDJ()

    watch_看广告_XFDJ()

    // open_开宝箱_XFDJ()

    // watch_看短剧_XFDJ()

    stopAllApp()
    // closeApp(PACK_NAME_XFDJ)
}